package com.moon.game.command.guest;

import java.util.HashMap;
import java.util.Random;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.message.ObjMessage;
import com.moon.game.service.GiftService;
import com.moon.game.service.MissionService;
import com.moon.game.service.PlayerItemService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;

/**
 * @description 创建人物
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-7-03 下午5:06:25
 */
public class CreatePlayer extends BaseCmd{
	private static final Logger log = Logger.getLogger(CreatePlayer.class);
	
	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
		case STATU_INIT:
			doInit(req);
			break;
		case STATU_CHECK:
			doResponse(req);
			break;
		default:
			break;
		}
	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		HashMap<String, PlayerVo> playerMap = PlayerService.doGetPlayerMap(req.getSid());
		if (playerMap != null && playerMap.size() > 0) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_BASE_PLAYER, "创建角色失败！"));
			return;
		}
		HashMap<String, Object> data = req.getClientRequest().getData();
		String sid = req.getSid();
		String playerId = UUID.randomUUID().toString();
		PlayerVo newPlayer = new PlayerVo();
		int now = GDateUtils.now();
		newPlayer.setId(playerId);
		newPlayer.setAccessTime(now);
		newPlayer.setCreateTime(now);
		newPlayer.setHp(100);
		newPlayer.setCurHp(100);
		newPlayer.setMp(50);
		newPlayer.setCurMp(50);
		newPlayer.setMaxRoom(100);
		newPlayer.setBurden(30);
		newPlayer.setAttackCold(10);
		newPlayer.setAttackHot(10);
		newPlayer.setDefenseCold(10);
		newPlayer.setDefenseHot(10);
		newPlayer.setExp(0);
		newPlayer.setGold(0);
		newPlayer.setMoney(0);
		newPlayer.setLastLoginTime(now);
		newPlayer.setLevel(1);
		newPlayer.setLoginTime(now);
		newPlayer.setSex(Integer.parseInt((String) data.get("sex")));
		newPlayer.setLocation(3000);
		newPlayer.setName((String) data.get("name"));
		newPlayer.setStatus(Constants.STATUS_ONLINE);
		newPlayer.setPetYardCapacity(Constants.DEFAULT_PET_NUM);
		newPlayer.setSid(sid);
		newPlayer.setId(playerId);
		PlayerService.doCreatePlayer(newPlayer);
		
		
		//TODO:测试调等级金币等
//		newPlayer.setLevel(40);
//		PlayerService.doAddGold(newPlayer.getSid(), newPlayer.getId(), 500000);
//		PlayerService.doAddMoney(newPlayer.getSid(), newPlayer.getId(), 500000);
		//...........................
		
		PlayerVo PlayerVo = PlayerService.doGetPlayer(sid, playerId);
		//发放礼包
		GiftService.addGift(sid, playerId, Constants.NEW_GIFT_ID, 1);
		GiftService.addGift(sid, playerId, Constants.DAY_GIFT_ID, 1);
		//分派任务
		MissionService.assignMission(PlayerVo, 1);
		//添加新手装备
		int key = new Random().nextInt(2);
		for (int i = 1000; i < 1010; i++) {
			PlayerItemService.addPlayerItem(sid, PlayerVo.getId(), PlayerItemService.playerItemFactory(i + key), false);
			i++;
		}
		ObjMessage resp = req.getClientResponse();
		resp.setVO(PAR_SID, newPlayer.getSid());
		resp.setVO(PAR_ID, newPlayer.getId());
		resp.setCmd("p_R");
		response(req, resp);
	}

}
